package com.jweb.framework.http.json;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jweb.framework.core.CoreRuntimeException;
import com.jweb.framework.core.channel.Transform;

/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：报文解析
 */
public class JsonEncoder implements Transform {
	
	private String encoding = "UTF8";
	
	public void setEncoding(String s){
		if("UTF-8".equals(s)){
			s = "UTF8";
		}
		else if("UTF-16BE".equals(s)){
			s = "UTF16_BE";
		}
		else if("UTF-16LE".equals(s)){
			s = "UTF16_LE";
		}
		else if("UTF-32BE".equals(s)){
			s = "UTF32_BE";
		}
		else if("UTF-32LE".equals(s)){
			s = "UTF32_LE";
		}
		else{
			throw new IllegalArgumentException("unsupported encoding , supported : UTF-8,UTF-16BE,UTF-16LE,UTF-32BE,UTF-32LE.");
		}
		this.encoding = s ;
	}
	
	@Override
	public boolean support(Object obj) {
		return obj!=null && (obj instanceof Map);
	}
	
	public Object transform(Object obj , String processId) throws CoreRuntimeException {
		try {
			return JSON.toJSONBytes(obj);
		} catch (Exception e) {
			throw new CoreRuntimeException("JAVAHT0030" , "encode message error" , e);
		}
	}
}
